<?php

namespace zhtfrontend\modules\teachergrowth\controllers;

use yii\console\Exception;
use yii\data\ActiveDataProvider;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\Comment;
use zhtfrontend\models\datas\CommentRelation;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\Teacher;
use zhtfrontend\modules\teachergrowth\components\rest\ActiveController;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class SiteController extends ActiveController
{
    public $modelClass = 'zhtfrontend\models\datas\Admin';

    public $serializer = [
        'class' => 'zhtfrontend\modules\teachergrowth\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionIndex(){
        $data = \Yii::$app->request->get();
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $teacher_id=ArrayHelper::keyExists('teacher_id', $data) ? $data['teacher_id'] : '';
        $role=ArrayHelper::keyExists('role', $data) ? $data['role'] : '';
        $teacher_info=Teacher::findOne($teacher_id);

        if ($role=="teache_teacher"){
            if (empty($teacher_info) || empty($teacher_id)){
                return ['statusCode' =>0, 'message' => 'error', 'result' => '当前教师不存在'];
            }else{
                //首先要统计考评督察的信息
                $teacher_info=Teacher::findOne($teacher_id);
                $mechanic_info=Mechanic::findOne($teacher_info->mechanic_id);
                $admin_info=Admin::find()->where(['id'=>$mechanic_info->admin_id])->all();
                foreach ($admin_info as $key=>$value){
                    $comment_info=Comment::find()->where(['valuator_type'=>2,'valuator_id'=>$user_id,'accepter_type'=>3,'accepter_id'=>$value->id])->one();
                    if (isset($comment_info)){
                       unset($admin_info[$key]);
                    }
                }
                $teacher_info_array=[
                    'teacher_id'=>$teacher_info->id,
                    'teacher_name'=>$teacher_info->name,
                    'teacher_img'=>$teacher_info->img,
                    'comment_count'=>count($admin_info),
                ];
                $items=[
                    'teacher_info'=>$teacher_info_array
                ];
                return ['statusCode' => 200, 'message' => 'success', 'items' => $items];
            }
        }else if ($role=="class_director"){
            //首先要统计考评督察的信息
            $mechanic=Mechanic::find()->select('id')->where(['admin_id'=>$user_id])->column();
            $mechanic_teacher=Teacher::find()->where(['mechanic_id'=>$mechanic])->all();
            foreach ($mechanic_teacher as $key=>$val){
                $comment_info=Comment::find()->where(['valuator_type'=>3,'valuator_id'=>$user_id,'accepter_type'=>2,'accepter_id'=>$val->id])->one();
                if (isset($comment_info)){
                    unset($mechanic_teacher[$key]);
                }
            }
            $admin=Admin::findOne($user_id);
            $teacher_info_array=[
                'teacher_id'=>'',
                'teacher_name'=>$admin->name,
                'teacher_img'=>'',
                'comment_count'=>count($mechanic_teacher),
            ];
            $items=[
                'teacher_info'=>$teacher_info_array
            ];
            return ['statusCode' => 200, 'message' => 'success', 'items' => $items];
        }else{
            return ['statusCode' => 0, 'message' => 'success', 'items' => '当前角色不存在'];
        }

    }

    public function actionTeacherlist(){
        $data = \Yii::$app->request->get();
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        if (empty($user_id)){
            return ['statusCode' =>0, 'message' => 'error', 'result' => '用户不存在'];
        }else{
            //查找老师负责的机构信息
            $mechanic=Mechanic::find()->select('id')->where(['admin_id'=>$user_id])->column();
            $mechanic_teacher=Teacher::find()->where(['mechanic_id'=>$mechanic])->all();
            $mechanic_teacher_array=[];
            foreach ($mechanic_teacher as $key=>$val){
                $sexiconBox=($val->sex==0)?'dmd-female-icon':'dmd-male-icon';
                $sexIcon=($val->sex==0)?'icon-female':'icon-male';
                $mechanic_name=Mechanic::findOne($val->mechanic_id);
                $mechanic_teacher_array[$key]=[
                    'teacher_id'=> $val->id,
                    'src'=>$val->img,
                    'teacherName'=>$val->name,
                    'sexiconBox'=>$sexiconBox,
                    'sexIcon'=>$sexIcon,
                    'institution'=>isset($mechanic_name)?$mechanic_name->name:'',
                ];
            }
            $items=[
                'mechanic_teacher_array'=>$mechanic_teacher_array,
                'teacherNum'=>count($mechanic_teacher_array),

            ];
            return ['statusCode' => 200, 'message' => 'success', 'items' => $items];
        }
    }

    public function actionLogin(){
        $data = \Yii::$app->request->get();
        $username = ArrayHelper::keyExists('username', $data) ? $data['username'] : '';
        $password = ArrayHelper::keyExists('password', $data) ? $data['password'] : '';
        if (empty($username) || empty($password)) {
            try {
                throw new ForbiddenHttpException('用户名或密码为空');
            } catch (ForbiddenHttpException $e) {
                return ['statusCode' => 0, 'message' => 'error', 'result' => '用户名或密码为空'];
            }
        }else{
            $role=['teache_teacher','class_director'];
            $modelClass = $this->modelClass;
            $model = $modelClass::find()->where(['username'=>$username,'role'=>$role])->one();
            if (empty($model)) {
                try {
                    throw new NotFoundHttpException('该用户不存在');
                } catch (NotFoundHttpException $e) {
                    return ['statusCode' => 0, 'message' => 'error', 'result' => '该用户不存在'];
                }
            }else {
                if (empty($model->password)){
                    try {
                        throw new NotFoundHttpException('该用户未设置密码，请先设置密码');
                    } catch (NotFoundHttpException $e) {
                        return ['statusCode' => 0, 'message' => 'error', 'result' => '该用户未设置密码，请先设置密码'];
                    }
                }
                if (\Yii::$app->getSecurity()->validatePassword($password, $model->password)) {
                    $cookies = \Yii::$app->response->cookies;
                    $cookies->add(new \yii\web\Cookie([
                        'name' => 'user_id',
                        'value' => $model->id,
                        'expire' => time() + 6004800
                    ]));
                    return ['items'=>$model];
                } else {
                    try {
                        throw new ForbiddenHttpException('密码错误');
                    } catch (ForbiddenHttpException $e) {
                        return ['statusCode' => 0, 'message' => 'error', 'result' => '密码错误'];
                    }
                }
            }
        }

    }




}